package aerouacmsla.interfazDeUsuario;

import aerouacmsla.entidad.Ciudad;
import aerouacmsla.procedimientos.BDConexion;
import aerouacmsla.procedimientos.CreateTables;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Marco
 */
public class PanelAdministrarCiudades extends javax.swing.JPanel {

    private BDConexion miConexion = null;
    private CreateTables modeloTabla = new CreateTables();

    public PanelAdministrarCiudades() {
        initComponents();
        if (getMiConexion() != null)
            jTable4.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery("SELECT * FROM ciudades;")));
    }

    public PanelAdministrarCiudades(BDConexion miConexion) {
        initComponents();
        setMiConexion(miConexion);
        jTable4.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery("SELECT * FROM ciudades;")));
    }

    public void setTabla(String SELECT){
        jTable4.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery(SELECT)));
    }

    public BDConexion getMiConexion() {
        return miConexion;
    }

    public void setMiConexion(BDConexion miConexion) {
        this.miConexion = miConexion;
    }

    public CreateTables getModeloTabla() {
        return modeloTabla;
    }

    public void setModeloTabla(CreateTables modeloTabla) {
        this.modeloTabla = modeloTabla;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        panelTabla3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable4 = new org.jdesktop.swingx.JXTable();
        panelBotones = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        textFieldBusqueda = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        radioCiudad = new javax.swing.JRadioButton();
        radioEstado = new javax.swing.JRadioButton();
        radioPais = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable4);

        javax.swing.GroupLayout panelTabla3Layout = new javax.swing.GroupLayout(panelTabla3);
        panelTabla3.setLayout(panelTabla3Layout);
        panelTabla3Layout.setHorizontalGroup(
            panelTabla3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
        );
        panelTabla3Layout.setVerticalGroup(
            panelTabla3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
        );

        panelBotones.setBorder(javax.swing.BorderFactory.createTitledBorder("Funciones"));

        jButton1.setText("Nueva Ciudad");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Modificar Ciudad");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Buscar Ciudad");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Ver Ciudades");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        buttonGroup1.add(radioCiudad);
        radioCiudad.setSelected(true);
        radioCiudad.setText("Ciudad");
        radioCiudad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioCiudadActionPerformed(evt);
            }
        });

        buttonGroup1.add(radioEstado);
        radioEstado.setText("Estado");
        radioEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioEstadoActionPerformed(evt);
            }
        });

        buttonGroup1.add(radioPais);
        radioPais.setText("Pais");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/IconoCiudad32px.png"))); // NOI18N
        jLabel1.setText("Ciudades");

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelBotonesLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioPais)
                            .addComponent(radioCiudad)
                            .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(textFieldBusqueda, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(radioEstado)))
                    .addGroup(panelBotonesLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(35, 35, 35)
                .addComponent(textFieldBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addComponent(radioCiudad)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioEstado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioPais)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelTabla3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelBotones, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                    .addComponent(panelTabla3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        setTabla("SELECT * FROM ciudades;");
    }//GEN-LAST:event_jButton4MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        VentanaNuevaCiudad nuevaCiudad = new VentanaNuevaCiudad(getMiConexion());
        nuevaCiudad.setVisible(true);
        setTabla("SELECT * FROM ciudades");
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        int fila = jTable4.getSelectedRow();
        if (fila > -1){
            Ciudad modificarCiudad = new Ciudad();
            //Trae el ID dela ciudad seleccionada
            String id = jTable4.getValueAt(fila, 0).toString();
            String SQL = "SELECT * FROM ciudades WHERE id_ciudad = '" + id + "';";
            
            getMiConexion().sendSQLQuery(SQL);
            try {
                if (getMiConexion().getResultado().next()) {
                    modificarCiudad.setId(id);
                    modificarCiudad.setCiudad(getMiConexion().getResultado().getObject("nombre_ciudad").toString());
                    modificarCiudad.setEstado(getMiConexion().getResultado().getObject("estado").toString());
                    modificarCiudad.setPais(getMiConexion().getResultado().getObject("pais").toString());
                    VentanaNuevaCiudad modificarUnaCiudad = new VentanaNuevaCiudad(modificarCiudad, getMiConexion());
                    modificarUnaCiudad.setVisible(true);
                    setTabla("SELECT * FROM ciudades");
                }
            } catch (SQLException ex) {
                Logger.getLogger(PanelAdministrarCiudades.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2MouseClicked

    private void radioCiudadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioCiudadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_radioCiudadActionPerformed

    private void radioEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioEstadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_radioEstadoActionPerformed

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        String SQL;
        if (radioCiudad.isSelected())
            SQL = "SELECT * FROM ciudades WHERE nombre_ciudad ='" + textFieldBusqueda.getText() + "'";
        else if (radioEstado.isSelected())
            SQL = "SELECT * FROM ciudades WHERE estado ='" + textFieldBusqueda.getText() + "'";
        else
            SQL = "SELECT * FROM ciudades WHERE pais ='" + textFieldBusqueda.getText() + "'";
        setTabla(SQL);
    }//GEN-LAST:event_jButton3MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable jTable4;
    private javax.swing.JPanel panelBotones;
    private javax.swing.JPanel panelTabla3;
    private javax.swing.JRadioButton radioCiudad;
    private javax.swing.JRadioButton radioEstado;
    private javax.swing.JRadioButton radioPais;
    private javax.swing.JTextField textFieldBusqueda;
    // End of variables declaration//GEN-END:variables

}
